package crypto

import (
	"crypto/rand"
	"math/big"
)

func (cry *Crypto) GenerateCode(min, max int) string {
	// 定义可用字符集（数字+大小写字母）
	charSet := []byte("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
	charSetLength := big.NewInt(int64(len(charSet)))
	// 生成长度在minLen和maxLen之间的随机数
	lengthRange := big.NewInt(int64(max - min + 1))
	lengthOffset, _ := rand.Int(rand.Reader, lengthRange)
	length := min + int(lengthOffset.Int64())

	// 生成验证码
	code := make([]byte, length)
	for i := 0; i < length; i++ {
		idx, _ := rand.Int(rand.Reader, charSetLength)
		code[i] = charSet[idx.Int64()]
	}
	return string(code)
}
